import * as vscode from 'vscode';
import * as _ from 'lodash';
import { createCc } from './cc';
import { createCp } from './cp';
import { camelCase, pascalCase } from 'change-case';

const pluginName = 'Vue3 helper' as const

/** 将用户输入标准化为合适的文件名 */
function normalizeNameToFileBasename(input: string): string {
    const trimmed = input.trim();
    // 替换空白为连字符，移除非法字符
    const replaced = trimmed
        .replace(/\s+/g, '-')
        .replace(/[^\w\-]/g, '')
        .replace(/-+/g, '-');
    // 避免以点或连字符开头
    const safe = replaced.replace(/^[\.-]+/, '');
    return safe || 'untitled';
}

// 插件激活时调用
export function activate(context: vscode.ExtensionContext) {
    console.log(`${pluginName} - 插件已激活`);

    try {
        // 新增组件
        const createComponent = vscode.commands.registerCommand(
            'vsx-vue-helper.createComponent',
            async (resource: vscode.Uri) => {
                try {
                    const baseFolder = resource as vscode.Uri;
                    if (!baseFolder || !baseFolder.fsPath) {
                        vscode.window.showErrorMessage('未获取到目标文件夹');
                        return;
                    }

                    const input = await vscode.window.showInputBox({
                        title: '请输入组件名称',
                        placeHolder: '如：MyButton | user-card | query-table',
                        validateInput: (val) => (val.trim() ? undefined : '名称不能为空')
                    });
                    if (!input) return;

                    const basename = pascalCase(normalizeNameToFileBasename(input));
                    await createCc(vscode.Uri.joinPath(baseFolder, `${basename}`));
                } catch (e: any) {
                    vscode.window.showErrorMessage(`创建组件失败：${e?.message || e}`);
                }
            }
        );

        // 新增页面
        const createPage = vscode.commands.registerCommand(
            'vsx-vue-helper.createPage',
            async (resource: vscode.Uri) => {
                try {
                    const baseFolder = resource as vscode.Uri;
                    if (!baseFolder || !baseFolder.fsPath) {
                        vscode.window.showErrorMessage('未获取到目标文件夹');
                        return;
                    }

                    const input = await vscode.window.showInputBox({
                        title: '请输入页面名称',
                        placeHolder: '如：Home | user-center | about',
                        validateInput: (val) => (val.trim() ? undefined : '名称不能为空')
                    });
                    if (!input) return;

                    const basename = pascalCase(normalizeNameToFileBasename(input));
                    await createCp(vscode.Uri.joinPath(baseFolder, `${camelCase(basename)}`));
                } catch (e: any) {
                    vscode.window.showErrorMessage(`创建页面失败：${e?.message || e}`);
                }
            }
        );

        context.subscriptions.push(createComponent, createPage);
    } catch (error) {
        console.error(`${pluginName} - 插件激活失败:`, error);
    }
}

// 插件停用时调用
export function deactivate() {
    console.log(`${pluginName} - 插件已停用`);
}
